//
//  ASHomeHeadFeaturedView.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/27.
//

#import "ASHomeHeadFeaturedView.h"
#import "ASDetailController.h"

static NSString *ASHomeHeadFeaturedSubCellStr = @"ASHomeHeadFeaturedSubCell";
@interface ASHomeHeadFeaturedSubCell : UICollectionViewCell
@property (strong, nonatomic) UIImageView *mainImage;
@end
@implementation ASHomeHeadFeaturedSubCell
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.mainImage];
        [self.mainImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
    }
    return self;
}
-(UIImageView *)mainImage{
    if (!_mainImage) {
        _mainImage = [UIImageView new];
        _mainImage.layer.masksToBounds = YES;
        _mainImage.layer.cornerRadius = 6;
        _mainImage.contentMode = UIViewContentModeScaleAspectFill;
        _mainImage.clipsToBounds = YES;
        [_mainImage setBackgroundColor:[ASTool backgroundRandomColor]];
    }
    return _mainImage;
}
@end

@implementation ASHomeHeadFeaturedView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.titleLab];
        [self.contentView addSubview:self.arrowImage];
        [self.contentView addSubview:self.numLab];
        [self.contentView addSubview:self.collectionView];
        [self.contentView addSubview:self.bottomLab];
        [self setAllViewLayout];
    }
    return self;
}

- (void)setAllViewLayout{
    @weakify(self);
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(13);
        make.height.mas_equalTo(45);
    }];
    
    [self.arrowImage mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerY.mas_equalTo(self.titleLab.mas_centerY);
        make.right.mas_equalTo(-13);
        make.size.mas_equalTo(CGSizeMake(6, 10));
    }];
    
    [self.numLab mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerY.mas_equalTo(self.titleLab.mas_centerY);
        make.right.mas_equalTo(self.arrowImage.mas_left).mas_offset(-5);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.top.mas_equalTo(self.titleLab.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(82); 
    }];
    
    [self.bottomLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        @strongify(self);
//        make.top.mas_offset(self.collectionView.mas_bottom).mas_offset(0);
//        make.height.mas_equalTo(45);
        make.left.mas_equalTo(13);
        make.top.mas_offset(140);
    }];
}

- (UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.font = [UIFont boldSystemFontOfSize:15];
        _titleLab.textColor = [UIColor colorWithHexString:@"#1A1A1A"];
        _titleLab.text = @"精选套图";
    }
    return _titleLab;
}

- (UILabel *)numLab{
    if (!_numLab) {
        _numLab = [UILabel new];
        _numLab.font = [UIFont systemFontOfSize:12];
        _numLab.textColor = [UIColor colorWithHexString:@"#808080"];
        _numLab.text = @"精选套图";
    }
    return _numLab;
}

- (UIImageView *)arrowImage{
    if (!_arrowImage) {
        _arrowImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_xjt"]];
    }
    return _arrowImage;
}

- (UILabel *)bottomLab{
    if (!_bottomLab) {
        _bottomLab = [UILabel new];
        _bottomLab.font = [UIFont boldSystemFontOfSize:15];
        _bottomLab.textColor = [UIColor colorWithHexString:@"#1A1A1A"];
        _bottomLab.text = @"每日精选";
    }
    return _bottomLab;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *fl = [UICollectionViewFlowLayout new];
        fl.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        fl.itemSize = CGSizeMake(80, 80);
        fl.minimumLineSpacing = 5;
        fl.sectionInset = UIEdgeInsetsMake(0, 13, 0, 13);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:fl];
        _collectionView.backgroundColor = self.contentView.backgroundColor;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[ASHomeHeadFeaturedSubCell class] forCellWithReuseIdentifier:ASHomeHeadFeaturedSubCellStr];
    }
    return _collectionView;
}

- (void)setModel:(ASHomeMainModel *)model{
    _model = model;
    self.numLab.text = [NSString stringWithFormat:@"%@ 图集",model.picture_num];
    [self.collectionView reloadData];
}

#pragma mark - UICollectionView
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.model.picture_urls.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ASHomeHeadFeaturedSubCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ASHomeHeadFeaturedSubCellStr forIndexPath:indexPath];
    if (self.model.picture_urls.count > indexPath.row) {
        NSString *str = self.model.picture_urls[indexPath.row];
        [cell.mainImage sd_setImageWithURL:[NSURL URLWithString:str]];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *arr = [NSMutableArray new];
    for (int x = 0; x < self.model.picture_urls.count; x++) {
        ASHomeMainModel *subM = [ASHomeMainModel new];
        subM.tID = self.model.tID;
        subM.avatar = self.model.avatar;
        subM.descriptionStr = self.model.descriptionStr;
        subM.nickname = self.model.nickname;
        subM.userid = self.model.userid;
        subM.is_collect = self.model.is_collect;
        subM.tag_arr = self.model.tag_arr;
        subM.thumb = self.model.picture_urls[x];
        [arr addObject:subM];
    }
    [ASTool addUMTongJiKey:@"home" subKey:@"home_top" titleStr:@"精选套图"];
    ASDetailController *detailVC = [ASDetailController new];
    detailVC.hidesBottomBarWhenPushed = YES;
    [detailVC createOldArr:arr showIndex:indexPath.row];
    [currViewController().navigationController pushViewController:detailVC animated:YES];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
